package cn.ekuma.epos.android.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/12/16.
 */
public abstract class AbstractQueryOneReturnAsyncTask<T,K>  extends AsyncTask<K,Integer,T> {
    private ProgressDialog progressDialog;
    private boolean isBack=false;
    boolean exeError=false;
    protected int errorCode=0;

    public AbstractQueryOneReturnAsyncTask(Context c){
        progressDialog = new ProgressDialog(c,0);
        progressDialog.setTitle("提示信息");
        progressDialog.setMessage("执行中，请稍后......");
        //    设置setCancelable(false); 表示我们不能取消这个弹出框，等下载完成之后再让弹出框消失
        progressDialog.setCancelable(false);
        //    设置ProgressDialog样式为水平的样式
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //    在onPreExecute()中我们让ProgressDialog显示出来
    }
    public AbstractQueryOneReturnAsyncTask(){
        this.isBack=true;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        if(!isBack)
            progressDialog.show();
    }
    @Override
    protected T doInBackground(K... params)
    {
        T  findObjs=null;
        try
        {
            findObjs=query(params[0]);
        }
        catch (Exception e)
        {
            exeError=true;
            e.printStackTrace();
        }
        finally
        {

        }
        return findObjs;
    }
    protected abstract T query(K params) throws Exception;

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        super.onProgressUpdate(values);
        //    更新ProgressDialog的进度条
        if(!isBack)
            progressDialog.setProgress(values[0]);
    }
    @Override
    protected  void onPostExecute(T result)
    {
        super.onPostExecute(result);
        if(!isBack)
            progressDialog.dismiss();
        if(exeError)
            queryError(errorCode);
        else
        if(result==null)
            queryNoFind();
        else
           queryResert(result);
    }

    protected abstract void queryResert(T result);

    protected abstract void queryNoFind();

    protected abstract void queryError(int type);

}